package com.myidis.servlet;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myidis.mapper.DeptMapper;
import com.myidis.mapper.QuotaMapper;
import com.myidis.mapper.QuotaTypeMapper;
import com.myidis.response.QuotaRes;
import com.myidis.entity.Department;
import com.myidis.entity.Quota;
import com.myidis.entity.QuotaType;

@Service
public class QuotaServlet {
	@Autowired
	private QuotaMapper quotaMapper;
	@Autowired
	private DeptMapper deptMapper;
	@Autowired
	private QuotaTypeMapper quotaTypeMapper;
	
	public List<QuotaRes> all() {
		return this.useful(quotaMapper.all());
	}
	
	private List<QuotaRes> useful(List<Quota> quotas) {
		List<QuotaRes> res = new ArrayList<QuotaRes>();
		for(Quota quota: quotas) {
			Department dept = deptMapper.getOne(quota.getDeptId());
			QuotaType type = quotaTypeMapper.getOne(quota.getTypeId());
			res.add(new QuotaRes(quota, type, dept));
		}
		return res;
	}
	
	public Quota getOne(int id) {
		return quotaMapper.getOne(id);
	}
	 
	public int insert(Quota obj) {
		return quotaMapper.insert(obj);
	}
	
	public int insertAll(List<Quota> objs) {
		int count = 0;
		for (Quota obj: objs) {
			count += quotaMapper.insert(obj);
		}
		return count;
	}
	
	public int update(Quota quota) {
		return quotaMapper.update(quota);
	}
	
	public int delete(int id) {
		return quotaMapper.delete(id);
	}
	
	public int deleteAll(int[] ids) {
		int count = 0;
		for(int id: ids)
			count += quotaMapper.delete(id);
		return count;
	}
}
